/*******************
This button.c file is for STC90C52 MCU 
controlling the fan of some buildings


Functions:
Reading the button status, give the command

*******************/
#include "STC89C51RC_RD_PLUS.H"
#include <intrins.h>
#include "user.h"

extern void display_test(void);
extern void display_all_on(void);
extern void display_all_clear(void);
extern void SendData(char dat);
extern void SendString(char *s);
extern void delayms(unsigned char ms);
extern void UartInit(void);
extern void init_normal_state(void);
extern void timer2_init(void);

extern unsigned char mod;
extern unsigned char b_flag;

//GPIO usage
sbit	LED=P0^4;//1: close the led;		0:open the led
sbit	RELAY_H=P4^1;//1:open the relay;		0:close the relay	,high speed fan control
sbit	RELAY_L=P2^7;//1:open the relay;		0:close the relay	,low speed fan control
sbit	BUTTON_UP=P2^2;//1:button up;		0:button down
sbit	BUTTON_DOWN=P1^4;
sbit	BUTTON_LEFT=P1^2;
sbit	BUTTON_RIGHT=P2^1;
sbit	BUTTON_ENTER=P1^3;
sbit 	SPEED=P3^2;	//fan speed control bit, input signal

/*
sbit	BUTTON_M1=P1^6;
sbit	BUTTON_M2=P1^7;
sbit	BUTTON_M3=P2^0;
sbit	BUTTON_M4=P1^0;
sbit	BUTTON_M5=P1^1;
*/
sbit	BUTTON_M1=P1^1;	//mode 1 ,no running
sbit	BUTTON_M2=P1^0; //mode 2 , 1/4
sbit	BUTTON_M3=P2^0;	//mode 3 , 1/2
sbit	BUTTON_M4=P1^7;	//mode 4 , 3/4
sbit	BUTTON_M5=P1^6;	//mode 5 , running continuously

sbit	BUTTON_PWR=P1^5;

char button_flag=0;	 //added only for test, corresponding variable is "unsigned char b_flag=1" defined in process.c

void led_off(void)
{
	LED=1;//1: close the led;		0:open the led
}

void led_on(void)
{
	LED=0;//1: close the led;		0:open the led
}

void relay_on(void)
{
	if(SPEED==0)
	{ //high speed mode
		RELAY_L=0;
		delayms(10);//for lower instant current, close one relay first		
		RELAY_H=1;//1:open the relay;		0:close the relay
	}
	else
	{ //low speed mode
		RELAY_H=0;//1:open the relay;		0:close the relay
		delayms(10);//for lower instant current, close one relay first
		RELAY_L=1;	
	}

}




void relay_off(void)
{	//close both high and low speed fans
	RELAY_H=0;//1:open the relay;		0:close the relay
	RELAY_L=0;
}

void BSP_Init(void)
{
unsigned char i=0,j=0;
	relay_off();
	led_on();
	UartInit();
	timer2_init();//timer2 for flash speed control
	SendString("Uart is OK,4800,8,0,0\n");
	mod=STATE_NORMAL;

//test phase
	display_all_on();
	for(j=0;j<50;j++)
		delayms(120);
	SendData(0x01);
	RELAY_H=1;//1:open the relay;		0:close the relay

	led_on();
	display_all_clear();
	for(j=0;j<50;j++)
		delayms(120);
	SendData(0x02);
	relay_off();
	led_off();

	display_all_on();
	for(j=0;j<50;j++)
		delayms(120);
	SendData(0x01);
	RELAY_L=1;//1:open the relay;		0:close the relay

	led_on();
	display_all_clear();
	for(j=0;j<50;j++)
		delayms(120);
	SendData(0x02);
	relay_off();
	led_off();
//end of test phase

//	display_test();

	init_normal_state();
	SendData(0x03);



}

//read if the button is pushed ,and give back the result value to indicate button number.
unsigned char read_button(void)
{
unsigned int tmp=0;

	if(BUTTON_UP==0)
		{
		delayms(20);
		if(BUTTON_UP==0)
			{
			return b_up;
			}
		
		}

	if(BUTTON_DOWN==0)
		{
		delayms(20);
		if(BUTTON_DOWN==0)
			{
			return b_down;
			}
		
		}
	
	if(BUTTON_LEFT==0)
		{
		delayms(20);
		if(BUTTON_LEFT==0)
			{
			return b_left;
			}
		
		}

	if(BUTTON_RIGHT==0)
		{
		delayms(20);
		if(BUTTON_RIGHT==0)
			{
			return b_right;
			}
		
		}

	if(BUTTON_ENTER==0)
		{
		delayms(20);
		if(BUTTON_ENTER==0)
			{
			tmp=0;
				while(1)
				{
					if(BUTTON_ENTER==0)
					{
						delayms(20);
						tmp++;
						if(tmp>200)
							return b_time;
					}
					if(BUTTON_ENTER==1)
						break;
				}
				return b_enter;
			}
		
		}

	if(BUTTON_M1==0)
		{
		delayms(20);
		if(BUTTON_M1==0)
			{
			return b_m1;
			}
		}
		
	if(BUTTON_M2==0)
		{
		delayms(20);
		if(BUTTON_M2==0)
			{
			return b_m2;
			}
		}

	if(BUTTON_M3==0)
		{
		delayms(20);
		if(BUTTON_M3==0)
			{
			return b_m3;
			}
		}

	if(BUTTON_M4==0)
		{
		delayms(20);
		if(BUTTON_M4==0)
			{
			return b_m4;
			}
		}

	if(BUTTON_M5==0)
		{
		delayms(20);
		if(BUTTON_M5==0)
			{
			return b_m5;
			}
		}

	if(BUTTON_PWR==0)
		{
		delayms(20);
		if(BUTTON_PWR==0)
			{
			return b_pwr;
			}
		}
	b_flag=1;
	return 0;//no button pushed
}







void button_test(void)
{

	if((BUTTON_UP==0)&&(button_flag==0))
		{
		SendString("up\n");
		button_flag=1;
		LED=0;//open led
		return;
		}

	if((BUTTON_DOWN==0)&&(button_flag==0))
		{
		SendString("down\n");
		button_flag=1;
		LED=1;//close led
		return;
		}

	if((BUTTON_LEFT==0)&&(button_flag==0))
		{
		SendString("left\n");
		button_flag=1;
		RELAY_H=1;//close relay
		return;
		}

	if((BUTTON_RIGHT==0)&&(button_flag==0))
		{
		SendString("right\n");
		button_flag=1;
		RELAY_H=0;//open relay
		return;
		}

	if((BUTTON_ENTER==0)&&(button_flag==0))
		{
		SendString("enter\n");
		button_flag=1;
		return;
		}

	if((BUTTON_M1==0)&&(button_flag==0))
		{
		SendString("m1\n");
		button_flag=1;
		return;
		}

	if((BUTTON_M2==0)&&(button_flag==0))
		{
		SendString("m2\n");
		button_flag=1;
		return;
		}

	if((BUTTON_M3==0)&&(button_flag==0))
		{
		SendString("m3\n");
		button_flag=1;
		return;
		}

	if((BUTTON_M4==0)&&(button_flag==0))
		{
		SendString("m4\n");
		button_flag=1;
		return;
		}

	if((BUTTON_M5==0)&&(button_flag==0))
		{
		SendString("m5\n");
		button_flag=1;
		return;
		}

	if((BUTTON_PWR==0)&&(button_flag==0))
		{
		SendString("power\n");
		button_flag=1;
		return;
		}

}


